﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace Krist_em_up.Components
{
    public class TextComponent : DrawableGameComponent
    { 
        public TextComponent(Game game, string fontName) 
            : base(game) 
        { 
            this.FontName = fontName;
            this.TextEffect = game.Content.Load<Effect>("Shaders/Gradient") ; 
            this.Text = ""; 
            this.TextColor = Color.White; 
            this.UpperLeft = Vector2.Zero; 
            this.RotateOrigin = Vector2.Zero; 
            this.Rotation = 0; 
            this.Scale = Vector2.One; 
            this.Effects = SpriteEffects.None; 
            this.LayerDepth = 1.0f;
        } 
 
        public void DoLoadContent() {
            Initialize();
            this.LoadContent(); 
        }

        protected override void LoadContent()
        {
            
            this.Font = Game.Content.Load<SpriteFont>(this.FontName); 
            this.Batch = new SpriteBatch(this.GraphicsDevice); 
            this.ShaderRenderTarget = new RenderTarget2D(this.GraphicsDevice, 
                                                         (int) this.Size.X, 
                                                         (int) this.Size.Y, 
                                                         1, 
                                                         this.GraphicsDevice.PresentationParameters.BackBufferFormat, 
                                                         this.GraphicsDevice.PresentationParameters.MultiSampleType, 
                                                         this.GraphicsDevice.PresentationParameters.MultiSampleQuality); 
            this.ShaderRenderTexture = new Texture2D(this.GraphicsDevice, 
                                                     (int) this.Size.X, 
                                                     (int) this.Size.Y, 
                                                     1, 
                                                     TextureUsage.None, 
                                                     ShaderRenderTarget.Format); 
 
            this.BackBufferTexture = new ResolveTexture2D(this.GraphicsDevice,
                                                          this.GraphicsDevice.PresentationParameters.BackBufferWidth,
                                                          this.GraphicsDevice.PresentationParameters.BackBufferHeight, 
                                                          1, 
                                                          ShaderRenderTarget.Format); 
        } 
 
        public override void Draw(GameTime gameTime) { 
            DrawText(this.UpperLeft, this.TextColor); 
        } 
 
        private void DrawText(Vector2 upperLeft, Color color) { 
            this.GraphicsDevice.ResolveBackBuffer(this.BackBufferTexture); 
            this.GraphicsDevice.SetRenderTarget(0, this.ShaderRenderTarget); 
            this.GraphicsDevice.Clear(new Color(0, 0, 0, 0)); 
 
            this.Batch.Begin(); 
            this.Batch.DrawString(this.Font, this.Text, Vector2.Zero, color); 
            this.Batch.End(); 
 
            this.GraphicsDevice.SetRenderTarget(0, null); 
            this.ShaderRenderTexture = this.ShaderRenderTarget.GetTexture(); 
 
            this.Batch.Begin(); 
            this.Batch.Draw(this.BackBufferTexture, Vector2.Zero, Color.White); 
            this.Batch.End(); 
 
            this.Batch.Begin(SpriteBlendMode.AlphaBlend, SpriteSortMode.Immediate, SaveStateMode.None); 
            if(this.TextEffect != null) { 
                this.TextEffect.Begin(); 
                this.TextEffect.CurrentTechnique.Passes[0].Begin(); 
            } 
            this.Batch.Draw(this.ShaderRenderTexture, upperLeft, Color.White); 
 
            if(this.TextEffect != null) { 
                this.TextEffect.CurrentTechnique.Passes[0].End(); 
                this.TextEffect.End(); 
            } 
            this.Batch.End(); 
        } 
 
        /// <summary> 
        /// Size of the text 
        /// </summary> 
        public Vector2 Size { 
            get { 
                return this.Font.MeasureString(this.Text); 
            } 
        } 
 
        /// <summary> 
        /// Text to be displayed 
        /// </summary> 
        public string Text; 
 
        /// <summary> 
        /// Color to draw the text in 
        /// </summary> 
        public Color TextColor; 
 
        /// <summary> 
        /// Upper-left corner of text drawing 
        /// </summary> 
        public Vector2 UpperLeft; 
 
        /// <summary> 
        /// Point around which to rotate 
        /// </summary> 
        public Vector2 RotateOrigin; 
 
        /// <summary> 
        /// Number of degrees to rotate text 
        /// </summary> 
        public float Rotation; 
 
        /// <summary> 
        /// Scale of the text 
        /// </summary> 
        public Vector2 Scale; 
 
        /// <summary> 
        /// Rotation effects 
        /// </summary> 
        public SpriteEffects Effects; 
 
        /// <summary> 
        /// Depth of the text 
        /// </summary> 
        public float LayerDepth; 
 
        /// <summary> 
        /// Name of the font 
        /// </summary> 
        private string FontName; 
 
        /// <summary> 
        /// Font 
        /// </summary> 
        private SpriteFont Font; 
 
        /// <summary> 
        /// Effect 
        /// </summary> 
        public Effect TextEffect; 
 
        /// <summary> 
        /// Batch 
        /// </summary> 
        private SpriteBatch Batch; 
 
        /// <summary> 
        /// Render target for shader text 
        /// </summary> 
        private RenderTarget2D ShaderRenderTarget; 
 
        private Texture2D ShaderRenderTexture; 
 
        private ResolveTexture2D BackBufferTexture; 
    } 
}
